
layui.use(['form', 'layer', 'table', 'laydate'], function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    var datalist_url = $('.datalist_url').val();
    var delete_url = $('.delete_url').val();
    var respond_url = $('.respond_url').val();


    // Table 列表
    var tableIns = table.render({
        elem: '#dataTable',
        url : datalist_url,
        page : true,
        cellMinWidth : 95,
        //height : "full-100",
        limit : 10,
        limits : [10, 20, 50, 100],
        id : "tableList",
        cols : [[
            {field: 'id', title: '序号', align:'center'},
            {field: 'account_number', title: '会员号', align:'center'},
            {field: 'product_name', title: '评价商品', align:'center'},
            {field: 'score', title: '评分', align:'left'},
            {field: 'content', title: '评价内容', align:'center'},
            {field: 'img_urls', title: '评价图片', align:"center", templet:function(data){
                var html = '';
                var img_urls = data.img_urls;
                if(img_urls){
                    for(var i = 0; i < img_urls.length; i++){
                        html += '<img src="'+img_urls[i].img_url+'" style="width: 40px;">';
                    }
                }

                return html;
            }},
            {field: 'create_date', title: '评价时间', align:'center', minWidth:110, sort: true},
            {title: '操作', fixed:"right", align:"center", minWidth:180, templet:function(data){
                var html = '';
                html += '<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="respond">回复</a>';
                html += '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>';

                return html;
            }}
        ]]
    });

    // 搜索
    $(".search_btn").on("click", function(){
        if($('.name').val() != ''){
            table.reload("tableList", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    account_number: $('.searchBox').find('input[name=account_number]').val()  // 搜索的关键字
                }
            });
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    // 清空搜索
    $(".reset_btn").on("click", function(){
        table.reload("tableList", {
            where: {
                account_number: ''
            }
        });
    });

    // 列表操作
    table.on('tool(dataTable)', function(obj){
        var layEvent = obj.event,
            _data = obj.data;

        // 删除
        if (layEvent === 'del') {
            layer.confirm('确定删除此数据？',{icon:3, title:'提示信息'},function(index){
                $.ajax({
                    url: delete_url,
                    data: {"id": _data.id},
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg('已删除', {icon: 1, time: 1000});
                            tableIns.reload();
                            layer.close(index);
                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg("服务器无响应");
                    }
                });
            });
        } else if(layEvent === 'respond'){
            layer.prompt({title: '回复评价', formType: 2}, function(text, index){
                var data = {
                    'responder_id': _data.id,
                    'content': text
                };
                $.ajax({
                    url: respond_url,
                    data: data,
                    type: "post",
                    dataType: "json",
                    success: function(res){
                        top.layer.close(index);
                        top.layer.msg(res.msg);
                        if(res.code == 1){
                            layer.closeAll("iframe");
                        }
                    },
                    error:function(data){
                        layer.msg("服务器无响应");
                    }
                });
            });
        }
    });

});